package com.yeyks.controller.admin;

import com.yeyks.account.dal.domain.AccountInfo;
import com.yeyks.account.service.AccountSetoutService;
import com.yeyks.annotation.AdminAppLog;
import com.yeyks.common.appcommon.dto.account.AppAccountDTO;
import com.yeyks.common.appcommon.dto.account.AppGetAccountDTO;
import com.yeyks.common.appcommon.json.JsonBankInfo;
import com.yeyks.common.appcommon.param.account.*;
import com.yeyks.common.appcommon.result.CommonPage;
import com.yeyks.common.appcommon.result.CommonResult;
import com.yeyks.common.utils.apputil.bean.MyBeanUtils;
import com.yeyks.commonReference.service.account.AccountInfoService;
import com.yeyks.controller.BaseController;
import com.yeyks.service.AppAccountInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

import java.util.List;

import static com.yeyks.common.constants.SysLogMsg.*;

/**
 * App 用户管理接口信息
 *
 * @author Administrator
 */
@Api(tags = "AppAccountController_用户管理接口", description = "用户管理相关接口[省,市,区,酒店]等")
@RestController
@RequestMapping("/admin/account")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AppAccountController extends BaseController {

    private final AppAccountInfoService appAccountInfoService;

    private final AccountInfoService accountInfoService;

    private final AccountSetoutService accountSetoutService;

    @ApiOperation("分页查询[省,市,区]相关的账户接口")
    @PostMapping("/query/list")
    public CommonResult<CommonPage<AppAccountDTO>> queryList(@Valid QueryAccountParam queryAccountParam) {

        AppAccountDTO appAccountDTO = validToken(queryAccountParam.getToken(),
                queryAccountParam.getVersion(), queryAccountParam.getPlatform());

        return accountInfoService.queryList(queryAccountParam, MyBeanUtils.convert(appAccountDTO, AccountInfo.class));

        //return appAccountInfoService.queryList(queryAccountParam,pageNum,pageSize);
    }

    @ApiOperation("分页查询子账户接口")
    @PostMapping("/query/child/list")
    public CommonResult<CommonPage<AppAccountDTO>> queryChildList(@Valid QueryChildAccountParam queryChildAccountParam) {

        AppAccountDTO appAccountDTO = validToken(queryChildAccountParam.getToken(),
                queryChildAccountParam.getVersion(), queryChildAccountParam.getPlatform());

        return accountInfoService.queryChildList(queryChildAccountParam, MyBeanUtils.convert(appAccountDTO, AccountInfo.class));
        //return appAccountInfoService.queryChildList(queryChildAccountParam);
    }

    @AdminAppLog(value = ACCOUNT_UPDATE_USE)
    @ApiOperation("启用/禁用账户")
    @PostMapping("/set/valid")
    public CommonResult setValid(@Valid SetAccountValidParam setAccountValidParam) {


        AppAccountDTO appAccountDTO = validToken(setAccountValidParam.getToken(),
                setAccountValidParam.getVersion(), setAccountValidParam.getPlatform());

        return accountInfoService.setValid(setAccountValidParam, MyBeanUtils.convert(appAccountDTO, AccountInfo.class),
                null);
        //return appAccountInfoService.setValid(setAccountValidParam);
    }

    @AdminAppLog(value = ACCOUNT_ENABLE_USE)
    @ApiOperation("启用账户_V1.6.1")
    @PostMapping("/set/enable")
    public CommonResult setEnable(@Valid SetAccountValidParam setAccountValidParam) {


        AppAccountDTO appAccountDTO = validToken(setAccountValidParam.getToken(),
                setAccountValidParam.getVersion(), setAccountValidParam.getPlatform());

        return accountInfoService.setValid(setAccountValidParam, MyBeanUtils.convert(appAccountDTO, AccountInfo.class),
                true);
        //return appAccountInfoService.setValid(setAccountValidParam);
    }

    @AdminAppLog(value = ACCOUNT_DISABLE_USE)
    @ApiOperation("禁用账户_V1.6.1")
    @PostMapping("/set/disable")
    public CommonResult setDisable(@Valid SetAccountValidParam setAccountValidParam) {


        AppAccountDTO appAccountDTO = validToken(setAccountValidParam.getToken(),
                setAccountValidParam.getVersion(), setAccountValidParam.getPlatform());

        return accountInfoService.setValid(setAccountValidParam, MyBeanUtils.convert(appAccountDTO, AccountInfo.class),
                false);
    }


    @ApiOperation("得到最新账户信息")
    @PostMapping("/get/account")
    public CommonResult<AppAccountDTO> getAccount(@Valid GetAccountParam getAccountParam) {

        AppAccountDTO appAccountDTO = validToken(getAccountParam.getToken(),
                getAccountParam.getVersion(), getAccountParam.getPlatform());

        return accountInfoService.getAccount(getAccountParam, MyBeanUtils.convert(appAccountDTO, AccountInfo.class));
    }

    @AdminAppLog(value = ACCOUNT_ADD_MSG)
    @ApiOperation("添加账户信息")
    @PostMapping("/add/account")
    public CommonResult addAccount(@Valid AddAccountParam addAccountParam) {

        return appAccountInfoService.addAccount(addAccountParam);
    }

    @AdminAppLog(value = ACCOUNT_UPDATE_MSG)
    @ApiOperation("编辑账户信息")
    @PostMapping("/update/account")
    public CommonResult updateAccount(@Valid UpdateAccountParam updateAccountParam) {

        return appAccountInfoService.updateAccount(updateAccountParam);
    }

    @ApiOperation("得到账户信息进行编辑")
    @PostMapping("/get")
    public CommonResult<AppGetAccountDTO> getAccountEdit(@Valid GetAccountEditParam getAccountEditParam) {

        return appAccountInfoService.getAccountEdit(getAccountEditParam);
    }


    @ApiOperation("获取账户的收款信息")
    @PostMapping("/get/pay")
    public CommonResult<JsonBankInfo> getPayAccountsByToken(@Valid GetPayAccountsParam getPayAccountsParam) {

        AppAccountDTO appAccountDTO = validToken(getPayAccountsParam.getToken(),
                getPayAccountsParam.getVersion(), getPayAccountsParam.getPlatform());

        return accountInfoService.getBankInfoByToken(getPayAccountsParam, MyBeanUtils.convert(appAccountDTO, AccountInfo.class));

        //return appAccountInfoService.getBankInfoByToken(getPayAccountsParam);
    }

    @ApiOperation("添加登录账户的设备信息")
    @PostMapping("/add/setout")
    public CommonResult addAccountSetout(@Valid AddAccountSetoutParam addAccountSetoutParam) {


        return accountSetoutService.addAccountSetout(addAccountSetoutParam);

        //return appAccountSetoutService.addAccountSetout(addAccountSetoutParam);
    }

    @ApiOperation("删除登录账户的设备信息")
    @PostMapping("/del/setout")
    public CommonResult delAccountSetout(@RequestParam(value = "sign", required = true) String sign,
                                         @RequestParam(value = "cid", required = true) String cid) {

        return accountSetoutService.delAccountSetout(sign, cid);
        //return appAccountSetoutService.delAccountSetout(sign, cid);
    }


    @ApiOperation("搜索开发商验证（1.7.0）")
    @PostMapping("/find/developer")
    public CommonResult<AppAccountDTO> findDeveloper(@Valid QueryDeveloperParam param) {

        validToken(param.getToken(),
                param.getVersion(), param.getPlatform());
        List<AccountInfo> accountInfoList = accountInfoService.findDeveloper(param.getName(), param.getDevelopType());
        if(!CollectionUtils.isEmpty(accountInfoList)){
            return CommonResult.success(MyBeanUtils.convert(accountInfoList.get(0), AppAccountDTO.class));
        }else{
            return CommonResult.failed("开发商输入名称有误");
        }
    }


}
